﻿using WorkOrder.Model;

namespace WorkOrder.Business
{
    public class Business_Table_User
    {
        DbHelper dbhelper = new DbHelper();
        public Model_Table_User Login(string userID, string password)
        {
            string sql = "select user_id,user_name,(case when len(password)>0 then '******' else '******' end) as password,remark from Table_User where user_name='" + userID + "' and password='" + password + "'";
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_User mod = new Model_Table_User(rdr["user_id"].ToString(), rdr["user_name"].ToString(), rdr["password"].ToString(), rdr["remark"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return mod;
                }
                else
                {
                    dbhelper.CloseDbConnection();
                    return null;
                }
            }
        }

        public bool ModifyPassword(string user_id, string user_login_id, string password)
        {
            string sql = "update Table_User set password='" + password + "' where user_id=" + user_id + " and user_name='" + user_login_id + "'";
            if (dbhelper.ExecuteNoQuery(sql) > 0)
                return true;
            else
                return false;
        }

        public Model_Table_User GetData(string keyValue)
        {
            string sql = "select * from Table_User where user_id=" + keyValue;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_User tmp = new Model_Table_User(rdr["user_id"].ToString(), rdr["user_name"].ToString(), rdr["password"].ToString(), rdr["remark"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else
                    return null;
            }
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="username">登录名</param>
        /// <param name="pwd">密码</param>
        /// <param name="remark">用户姓名</param>
        /// <param name="userType">用户类型，0:用户,1:访客,2:管理员</param>
        /// <returns></returns>
        public bool AddUser(string username, string pwd, string remark, int userType, bool workorder, bool changeover, bool maint, bool notice)
        {
            string sqlstr = "insert into Table_User (user_name,password,remark) values ('" + username + "','" + pwd + "','" + remark + "'); ";
            sqlstr += "declare @tmp int; select @tmp=user_id from Table_User where user_name='" + username + "'; ";
            
            if (userType == 0) //用户
            {
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Area',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Department',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Exception_type',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Step',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Order_type',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Permission',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Process',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Product',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Town',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_User',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Factory',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Integrated_unit',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Debugging_unit',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Supervision_unit',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Changeover_Status',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Changeover_problems_type',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_off_day',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Reason_type',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Other',0,0,0,1);";
                if (workorder)
                {
                    sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Work_order',1,1,1,1); ";
                    sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Exception',1,1,1,1); ";
                    sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Order_record',1,1,1,1); ";
                }
                if (changeover)
                {
                    sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Changeover',1,1,1,1);";
                    sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Cut_Problems',1,1,1,1);";
                }
                if (maint)
                {
                    sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Customer_Info',1,1,1,1);";
                    sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Maintenance',1,1,1,1);";
                }
                if (notice)
                {
                    sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Notice',1,1,1,1);";
                }
            }
            else if (userType == 1)  //访客
            {
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Area',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Department',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Exception_type',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Step',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Order_type',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Permission',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Process',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Product',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Town',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_User',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Factory',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Integrated_unit',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Debugging_unit',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Supervision_unit',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Changeover_Status',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Changeover_problems_type',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_off_day',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Reason_type',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Other',0,0,0,1);";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Work_order',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Exception',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Order_record',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values (@tmp,'Table_Changeover',0,0,0,1);";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Cut_Problems',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Customer_Info',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Maintenance',0,0,0,1);";
            }
            else if (userType == 2) //管理员
            {
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Area',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Department',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Exception_type',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Step',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Order_type',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Permission',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Process',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Product',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Town',1,1,1,1); ";
                sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_User',1,1,1,1); ";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Factory',1,1,1,1);";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Integrated_unit',1,1,1,1);";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Debugging_unit',1,1,1,1);";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Supervision_unit',1,1,1,1);";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Changeover_Status',1,1,1,1);";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Changeover_problems_type',1,1,1,1);";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_off_day',1,1,1,1);";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Reason_type',1,1,1,1);";
                sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Other',1,1,1,1);";

                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Work_order',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Exception',0,0,0,1); ";
                //sqlstr += "insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query)values(@tmp,'Table_Order_record',0,0,0,1); ";

                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Changeover',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Cut_Problems',0,0,0,1);";

                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Customer_Info',0,0,0,1);";
                //sqlstr += "insert into Table_Permission([user_id],[table_name],[permission_Insert],[permission_Delete],[permission_Modify],[permission_Query]) values(@tmp,'Table_Maintenance',0,0,0,1);";
            }

            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
    }
}
